package com.yuyou.fn.report.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.report.vo.StudentBalanceQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import com.yuyou.fn.report.mapper.StudentBalanceMapper;
import com.yuyou.fn.report.entity.StudentBalance;
import com.yuyou.fn.report.service.IStudentBalanceService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import java.util.List;

/**
 *
 * StudentBalance 表数据服务层接口实现类
 *
 */
@Service
public class StudentBalanceServiceImpl extends SuperServiceImpl<StudentBalanceMapper, StudentBalance,Long> implements IStudentBalanceService {

    @Override
    public Page<StudentBalance> findStudentBalancePage(DataPermission dataPermission,StudentBalanceQueryVo studentBalanceQueryVo, int pageNo, int pageSize) {
        Example<StudentBalance> studentBalanceExample=StudentBalance.createExample();
        Example.Criteria criteria=studentBalanceExample.createCriteria();
        if (studentBalanceQueryVo.getLastCampusId()!=null){
            criteria.andCondition("(last_campus_id="+studentBalanceQueryVo.getLastCampusId()+" or last_campus_id is null)");
        }else if (Utils.checkListNotEmpty(dataPermission.getCampusIds())){
            StringBuffer stringBuffer=new StringBuffer();
            stringBuffer.append("(last_campus_id is null ");
            stringBuffer.append(" or last_campus_id in ("+StringUtils.join(dataPermission.getCampusIds(),',')+")");
            stringBuffer.append( ")");
            criteria.andCondition(stringBuffer.toString());
        }else {
            criteria.andIsNull("lastCampusId");
        }
        if (StringUtils.isNotEmpty(studentBalanceQueryVo.getStudentName())){
            criteria.andLike("studentName","%"+studentBalanceQueryVo.getStudentName()+"%");
        }
        if (StringUtils.isNotEmpty(studentBalanceQueryVo.getStudentPhoneNo())){
            criteria.andLike("studentPhoneNo","%"+studentBalanceQueryVo.getStudentPhoneNo()+"%");
        }
        studentBalanceExample.orderBy("lastCampusId");
        return this.findPageByExample(studentBalanceExample,pageNo,pageSize);
    }


    @Override
    public Workbook exportStudentBalance(DataPermission dataPermission,StudentBalanceQueryVo studentBalanceQueryVo) {
        Page<StudentBalance> studentBalancePage=this.findStudentBalancePage(dataPermission,studentBalanceQueryVo,1,50000);

        SXSSFWorkbook studentRefundExcel = studentBalance2excel(studentBalancePage.getRecords());
        return studentRefundExcel;
    }


    public SXSSFWorkbook studentBalance2excel(List<StudentBalance> studentBalanceList) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("学员余额");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[] { "学生姓名","学生电话","学号","最近报读校区","在读期数","退费期数（只统计退余额并且剔除转班）","就读年级","就读学校","账户余额" };

        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        if (studentBalanceList!=null){
            for (int i = 0; i < studentBalanceList.size(); i++) {
                SXSSFRow row = sheet.createRow(i + 1);
                StudentBalance studentBalance = studentBalanceList.get(i);
                int columnIndex = 0;
                SXSSFCell cell = row.createCell(columnIndex++);
                //cell.setCellType(CellType.STRING);
                cell.setCellValue(String.valueOf(studentBalance.getStudentName()));

                cell = row.createCell(columnIndex++);
                cell.setCellValue(studentBalance.getStudentPhoneNo());

                cell = row.createCell(columnIndex++);
                cell.setCellValue(studentBalance.getStudentNo());


                cell = row.createCell(columnIndex++);
                cell.setCellValue(studentBalance.getLastCampusName());

                cell = row.createCell(columnIndex++);
                cell.setCellValue(StringUtils.defaultString(studentBalance.getRegPeriodNames()));

                cell = row.createCell(columnIndex++);
                cell.setCellValue(StringUtils.defaultString(studentBalance.getRefundPeriodNames()));

                cell = row.createCell(columnIndex++);
                cell.setCellValue(StringUtils.defaultString(studentBalance.getGradeName()));

                cell = row.createCell(columnIndex++);
                cell.setCellValue(studentBalance.getSchool());

                cell = row.createCell(columnIndex++);
                cell.setCellType(CellType.NUMERIC);
                cell.setCellValue(studentBalance.getBalanceAmount() == null ? 0d : studentBalance.getBalanceAmount().doubleValue());


            }
        }
        return excel;
    }

}